import request from '@/utils/request'
import moment from 'moment'

// 速运下订单
export async function createFastFreightOrder(params) {
  const { success, data } = await request({
    url: '/admin/api/sfexpress/createExpressOrder',
    method: 'post',
    data: params
  })
  if (!success || data?.errorCode !== 'S0000') return { code: 0, data: '', msg: data?.errorMsg };
  const { waybillNoInfoList: [info] } = data?.msgData || {}
  return { code: 200, data: Object.assign((data?.msgData || {}), info), msg: data?.errorMsg };
}

// 冷运运输订单接收-冷运下单
export async function createColdFreightOrder(params) {
  const { success, data } = await request({
    url: '/admin/api/sfexpress/receiveColdOrder',
    method: 'post',
    data: params
  })
  if (!success || +data?.code !== 0) return { code: 0, data: '', msg: data?.message };
  const { sfOrderNo } = data?.data || {}
  return { code: 200, data: Object.assign(data?.data || {}, { waybillNo: sfOrderNo }), msg: data?.message }
}

// 快递下单
export async function createExpressOrder(params) {
  if (params?.consignee?.province?.includes('广东')) {
    const coldParams = {
      erpOrder: params.orderId,
      productCode: 'SE0004',
      monthlyAccount: params.monthlyCard,
      paymentTypeCode: 'PR_ACCOUNT',
      shipperContactName: params.shipper.contact,
      shipperContactTel: params.shipper.mobile,
      shipperProvinceName: params.shipper.province,
      shipperCityName: params.shipper.city,
      shipperDistrictName: params.shipper.county,
      shipperLocationName: params.shipper.address,
      requirePickupTimeFm: moment().format('YYYY-MM-DD HH:mm:ss'),
      consigneeContactName: params.consignee.contact,
      consigneeContactTel: params.consignee.mobile,
      consigneeProvinceName: params.consignee.province,
      consigneeCityName: params.consignee.city,
      consigneeDistrictName: params.consignee.county,
      consigneeLocationName: params.consignee.address,
      transportType: 'LAND',
      temperatureLevelCode: '2',
      totalWeight: params.totalWeight,
      remark: params.remark,
      orderTime: moment().format('YYYY-MM-DD HH:mm:ss'),
    }
    return createColdFreightOrder(coldParams)
  }

  const fastParams = {
    language: 'zh-CN',
    orderId: params.orderId,
    cargoDetails: params.cargoDetails,
    cargoDesc: params.cargoDesc,
    totalWeight: params.totalWeight,
    contactInfoList: [
      params.shipper,
      params.consignee
    ],
    monthlyCard: params.monthlyCard,
    payMethod: 1
  }
  return createFastFreightOrder(fastParams)
}

// 查询快递面单
export async function getExpressSheet(documents = []) {
  const { success, data } = await request({
    url: '/admin/api/sfexpress/cloudPrintBillToPdf',
    method: 'post',
    data: {
      templateCode: 'fm_76130_standard_CZKJS9IQ353P',
      version: "2.0",
      fileType: "pdf",
      sync: true,
      documents
    }
  })
  if (!success || !data.success) return { code: 0, data: '', msg: data.errorMessage };
  return { code: 200, data: data?.obj?.files || [], msg: '操作成功' }
}

// 发货
export async function sendExpressOrder(params) {
  return request({
    url: '/admin/shipping/delivery',
    method: 'put',
    params
  })
}

// 获取面单
export async function getExpressOrder(params) {
  return request({
    url: '/admin/shipping/print',
    method: 'post',
    data: params
  })
}

// 查询快递路由
export async function queryExpressRoute(params) {
  return request({
    url: '/admin/shipping/routers',
    method: 'post',
    data: params
  })
}

